<?php
/**
 * Created by PhpStorm.
 * User: 666
 * Date: 2020/4/3
 * Time: 16:24
 */
namespace App\Process;

use App\Consts\RedisKey;
use App\IpLimit\IpList;
use App\Middleware\SignValidationMiddleware;
use App\Model\User;
use App\Utility\Register;
use App\Utility\Response;
use EasySwoole\Component\Process\AbstractProcess;
use EasySwoole\EasySwoole\Config;
use EasySwoole\RedisPool\Redis;

class IpAccessCount extends AbstractProcess
{



    protected function run($arg)
    {


        $ipconfig = Config::getInstance()->getConf('ipAccessCount');

        $this->addTick($ipconfig['interval']*1000, function ()use ($ipconfig){

            $list = IpList::getInstance()->accessList($ipconfig['count']);

            IpList::getInstance()->clear();
            foreach ($list as $ip){

                IpList::getInstance()->violationIp($ip['key']);
            }

        });


//        go(function (){
//
//            $users = User::create()->all();
//            foreach($users as $user){
//
//                go(function () use ($user){
//
//                    $info = ['mobile'=> $user->mobile ,'password'=> 123456,'sessid'=> '222545' ,'image_code'=> 1234,'ip'=> '192.168.1.1' ];
//                    $info['sign'] = SignValidationMiddleware::getInstance()->getSign($info , "");
//
//                    $cli = new \Swoole\Coroutine\Http\Client('127.0.0.1', 9101);
//                    $cli->post('/v1/login',$info);
//                    $res = json_decode($cli->body ,true);
//
//                    $cli->close();
//                    if($res['code'] == 200){
//
//                        $client = new \Swoole\Coroutine\Http\Client("127.0.0.1", 9101);
//                        $ret = $client->upgrade("/?token=".$res['data']['token']);
//                        if ($ret) {
//
//                            while(true) {
////                          var_dump($client->recv()->fd);
//                                \co::sleep(2);
//
//                            }
//                        }
//
//                    }
//                });
//                \Co::sleep(0.1);
//            }
//
//        });

    }




    protected function onPipeReadable(\Swoole\Process $process)
    {
        /*
         * 该回调可选
         * 当有主进程对子进程发送消息的时候，会触发的回调，触发后，务必使用
         * $process->read()来读取消息
         */
    }

    protected function onShutDown()
    {
        /*
         * 该回调可选
         * 当该进程退出的时候，会执行该回调
         */
    }

    protected function onException(\Throwable $throwable, ...$args)
    {
        /*
         * 该回调可选
         * 当该进程出现异常的时候，会执行该回调
         */
    }
}